<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="prev" href="module-Queue.html" />
<link rel="parent" href="module-Queue.html" />
<link rel="next" href="module-weakref.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>5.10.1 Queue Objects</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.10 queue  "
  href="module-Queue.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="5.10 queue  "
  href="module-Queue.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="5.11 weakref  "
  href="module-weakref.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-Queue.html">5.10 Queue  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-Queue.html">5.10 Queue  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-weakref.html">5.11 weakref  </a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION0071010000000000000000"></a>
<a name="QueueObjects"></a>
<br>
5.10.1 Queue Objects
</h2>

<p>
Class <tt class="class">Queue</tt> implements queue objects and has the methods
described below.  This class can be derived from in order to implement
other queue organizations (e.g. stack) but the inheritable interface
is not described here.  See the source code for details.  The public
methods are:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-807' xml:id='l2h-807' class="method">qsize</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Return the approximate size of the queue.  Because of multithreading
semantics, this number is not reliable.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-808' xml:id='l2h-808' class="method">empty</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Return <code>True</code> if the queue is empty, <code>False</code> otherwise.
Because of multithreading semantics, this is not reliable.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-809' xml:id='l2h-809' class="method">full</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Return <code>True</code> if the queue is full, <code>False</code> otherwise.
Because of multithreading semantics, this is not reliable.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-810' xml:id='l2h-810' class="method">put</tt></b>(</nobr></td>
  <td><var>item</var><big>[</big><var>, block</var><big>[</big><var>, timeout</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Put <var>item</var> into the queue. If optional args <var>block</var> is true
and <var>timeout</var> is None (the default), block if necessary until a
free slot is available. If <var>timeout</var> is a positive number, it
blocks at most <var>timeout</var> seconds and raises the <tt class="exception">Full</tt>
exception if no free slot was available within that time.
Otherwise (<var>block</var> is false), put an item on the queue if a free
slot is immediately available, else raise the <tt class="exception">Full</tt>
exception (<var>timeout</var> is ignored in that case).

<p>

<span class="versionnote">New in version 2.3:
the timeout parameter.</span>

<p>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-811' xml:id='l2h-811' class="method">put_nowait</tt></b>(</nobr></td>
  <td><var>item</var>)</td></tr></table></dt>
<dd>
Equivalent to <code>put(<var>item</var>, False)</code>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-812' xml:id='l2h-812' class="method">get</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>block</var><big>[</big><var>, timeout</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Remove and return an item from the queue. If optional args
<var>block</var> is true and <var>timeout</var> is None (the default),
block if necessary until an item is available. If <var>timeout</var> is
a positive number, it blocks at most <var>timeout</var> seconds and raises
the <tt class="exception">Empty</tt> exception if no item was available within that
time. Otherwise (<var>block</var> is false), return an item if one is
immediately available, else raise the <tt class="exception">Empty</tt> exception
(<var>timeout</var> is ignored in that case).

<p>

<span class="versionnote">New in version 2.3:
the timeout parameter.</span>

<p>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-813' xml:id='l2h-813' class="method">get_nowait</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Equivalent to <code>get(False)</code>.
</dl>

<p>
Two methods are offered to support tracking whether enqueued tasks have
been fully processed by daemon consumer threads.

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-814' xml:id='l2h-814' class="method">task_done</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Indicate that a formerly enqueued task is complete.  Used by queue consumer
threads.  For each <tt class="method">get()</tt> used to fetch a task, a subsequent call to
<tt class="method">task_done()</tt> tells the queue that the processing on the task is complete.

<p>
If a <tt class="method">join()</tt> is currently blocking, it will resume when all items
have been processed (meaning that a <tt class="method">task_done()</tt> call was received
for every item that had been <tt class="method">put()</tt> into the queue).

<p>
Raises a <tt class="exception">ValueError</tt> if called more times than there were items
placed in the queue.

<span class="versionnote">New in version 2.5.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-815' xml:id='l2h-815' class="method">join</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Blocks until all items in the queue have been gotten and processed.

<p>
The count of unfinished tasks goes up whenever an item is added to the
queue. The count goes down whenever a consumer thread calls <tt class="method">task_done()</tt>
to indicate that the item was retrieved and all work on it is complete.
When the count of unfinished tasks drops to zero, join() unblocks.

<span class="versionnote">New in version 2.5.</span>

</dl>

<p>
Example of how to wait for enqueued tasks to be completed:

<p>
<div class="verbatim"><pre>
    def worker(): 
        while True: 
            item = q.get() 
            do_work(item) 
            q.task_done() 

    q = Queue() 
    for i in range(num_worker_threads): 
         t = Thread(target=worker)
         t.setDaemon(True)
         t.start() 

    for item in source():
        q.put(item) 

    q.join()       # block until all tasks are done
</pre></div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.10 queue  "
  href="module-Queue.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="5.10 queue  "
  href="module-Queue.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="5.11 weakref  "
  href="module-weakref.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-Queue.html">5.10 Queue  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-Queue.html">5.10 Queue  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-weakref.html">5.11 weakref  </a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
